package SinglePlayer;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MouseEvent;
import org.w3c.dom.views.AbstractView;

public class CardComponent extends JLabel {

	public static int count;
	private static int[] s1,s2 = null;
	private Card card;
	private int select; // 0 >> non-select, 1 >> selected, 2 >> FaceUp
	private GUI gui;
	private Timer timer;
	private ImageIcon imgCard;
	
	public CardComponent(String name, Card card, GUI gui) {
		super(name);
		this.gui=gui;
		this.card = card;
		this.setOpaque(true);
		imgCard = new ImageIcon(this.getClass().getResource("/images/card.jpg"));
		this.setIcon(imgCard);
		this.setBackground(Color.blue);
		
		Timer timer = new Timer(2000, new FaceUpAction(gui));
		timer.setRepeats(false);
		this.addMouseListener(new ClickAction(this,gui,timer));
	}
	
	public Card getCard()
	{
		return card;
	}
	
	public int getSelect() {
		return select;
	}

	
	public static void clearSelect()
	{
		s1=null;
		s2=null;
		count=0;
	}
	
	public void setSelect(int x) {
		select = x;
		if (x==0)
		{
			this.setIcon(imgCard);
		}
		else if (x==1)
		{
			this.setIcon(card.getValue());
			this.setBackground(Color.red);
		}
		else if (x==2)
		{
			this.setIcon(card.getValue());
			this.setBackground(Color.red);
		}
		
	}
	
	public static int[] getCardSelect(int x)
	{
		if (x==1)
			return s1;
		else if (x==2)
			return s2;
		else
			return null;
	}

	
	class ClickAction implements MouseListener {
		private CardComponent cc;
		private GUI gui;
		private GameConsole gameConsole;
		private Timer timer;
		
		public ClickAction(CardComponent cc,GUI gui,Timer timer) {
			this.cc = cc;
			this.gui=gui;
			this.gameConsole=gui.getGameConsole();
			this.timer=timer;
		}
		
		@Override
		public void mouseClicked(java.awt.event.MouseEvent e) {
			if (GameConsole.getTurn()!=Turn.Wait)
			{
				
			// TODO Auto-generated method stub
			if (cc.getSelect()==0)
			{
				if (count<2)
				{
					cc.setSelect(1);
					count++;
					if (s1==null)
						s1=cc.getCard().getLocation();
					else if (s2==null)
						s2=cc.getCard().getLocation();
				}
			}
			/*
			else if (cc.getSelect()==1)
			{
				cc.setSelect(0);
				count--;

				if (s1!=null)
				if (s1[0]==cc.getCard().getLocation()[0] && s1[1]==cc.getCard().getLocation()[1])
				{
					s1=null;
				}
				
				if (s2!=null)
				if (s2[0]==cc.getCard().getLocation()[0] && s2[1]==cc.getCard().getLocation()[1])
				{
					System.out.println("s2 = null");
					s2=null;
				}	
			}
			*/
			
			if (CardComponent.getCardSelect(1)!=null && CardComponent.getCardSelect(2)!=null)
			{
				System.out.println("Card 1 : " + gui.getCard(CardComponent.getCardSelect(1)).getValue());
				System.out.println("Card 2 : " + gui.getCard(CardComponent.getCardSelect(2)).getValue());
				if (gui.getCard(CardComponent.getCardSelect(1)).getValue() == gui.getCard(CardComponent.getCardSelect(2)).getValue())
				{
					if (gameConsole.getTurn()==Turn.P1)
					{
						gui.plusScore(0);
					}
					else if (gameConsole.getTurn()==Turn.P2)
					{
						gui.plusScore(1);
					}
					gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(2);
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(2);
					gui.clear();

					gui.updateTurn();
				}
				else 
				{
					GameConsole.setPreTurn(GameConsole.getTurn());
					if (gameConsole.getTurn()==Turn.P1)
					{
						gameConsole.setTurn(Turn.P2);
						gameConsole.setPreTurn(Turn.P2);
					}
					else if (gameConsole.getTurn()==Turn.P2)
					{
						gameConsole.setTurn(Turn.P1);
						gameConsole.setPreTurn(Turn.P1);
					}
					GameConsole.setTurn(Turn.Wait);
					gui.updateTurn();
					
					gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(2);
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(2);
					
						timer.start();
				}
			}
			
			System.out.println(cc.getCard()+"");
			System.out.println(s1+"");
			System.out.println(s2+"");
			}
		}

		@Override
		public void mouseEntered(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(java.awt.event.MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

	}
	class FaceUpAction implements ActionListener
	{
		private GUI gui;

		public FaceUpAction(GUI gui)
		{
			this.gui=gui;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if (gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]]!=null &&
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]]!=null)
			{
			gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(0);
			gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(0);
			
			}
			gui.clear();
			GameConsole.setTurn(GameConsole.getPreTurn());
			gui.updateTurn();
			
		}
		
	}
}
